# 外星人入侵游戏

import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from game_stats import GameStats
from button import Button
from score_board import ScoreBoard
import game_functions as gf
"""游戏"""


def run_game():  # 运行游戏函数
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()  # 创建实例
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")  # 标题
    # 创建一艘飞船
    ship = Ship(ai_settings, screen)
    # 创建一个用于存储子弹的编组
    bullets = Group()
    # 创建外星人群
    aliens = Group()
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # 创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)
    # 创建计分板
    sb = ScoreBoard(ai_settings, screen, stats)
    # 创建play按钮
    play_button = Button(ai_settings, screen, "Play")
    # 开始游戏主循环
    while True:
        gf.check_events(ai_settings, screen, ship, bullets, play_button, stats, aliens, sb)
        if stats.game_active:
            ship.update()
            gf.update_bullets(aliens, bullets, ai_settings, screen, ship, stats, sb)
            gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets, sb)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets, stats, play_button, sb)


run_game()
